package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.system.dao.QuaStatisticsDao;
import com.ruoyi.system.domain.dto.QuaStatisticsDto;
import com.ruoyi.system.domain.po.PmsArchivesExample;
import com.ruoyi.system.domain.vo.QuaStatisticsVo;
import com.ruoyi.system.mapper.PmsArchivesMapper;
import com.ruoyi.system.service.QuaStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static cn.hutool.core.date.DateField.ERA;
import static com.ruoyi.common.constant.UserConstants.QuestionCode.*;

@Service
@Slf4j
public class QuaStatisticsServiceImpl implements QuaStatisticsService {

    @Resource
    private QuaStatisticsDao quaStatisticsDao;
    @Resource
    private PmsArchivesMapper pmsArchivesMapper;

    @Override
    public List<QuaStatisticsVo.JmrsResult> queryJmrs(QuaStatisticsDto.QueryPatient dto) throws ParseException {
        List<QuaStatisticsVo.JmrsResult> resultList = new ArrayList<>();
        List<QuaStatisticsVo.JmrsList> xgzlList = quaStatisticsDao.selectJmrs(dto.getTimeType(), dto.getStartTime(), dto.getEndTime());
        if (CollectionUtil.isNotEmpty(xgzlList)) {
            Map<String, List<QuaStatisticsVo.JmrsList>> map = xgzlList.stream().collect(Collectors.groupingBy(QuaStatisticsVo.JmrsList::getDateTime));
            for (Map.Entry<String, List<QuaStatisticsVo.JmrsList>> entry : map.entrySet()) {
                QuaStatisticsVo.JmrsResult result = new QuaStatisticsVo.JmrsResult();
                Map<Long, List<QuaStatisticsVo.JmrsList>> listMap = entry.getValue().stream().collect(Collectors.groupingBy(QuaStatisticsVo.JmrsList::getFirstAidId));
                for (Map.Entry<Long, List<QuaStatisticsVo.JmrsList>> rptEntry : listMap.entrySet()) {
                    List<QuaStatisticsVo.JmrsList> value = rptEntry.getValue();
                    for (QuaStatisticsVo.JmrsList jmrsList : value) {
                        if (jmrsList.isFlag()) {
                            result.setGailv(result.getGailv() + 1);
                            break;
                        }
                    }
                }
                result.setTotal(entry.getValue().stream().collect(Collectors.groupingBy(QuaStatisticsVo.JmrsList::getFirstAidId)).size());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-ww");
                Date parse = new Date();
                String key = entry.getKey();
                if (StrUtil.isEmpty(key)) {
                    continue;
                }
                switch (dto.getTimeType()) {
                    case 0:
                        sdf = new SimpleDateFormat("yyyy-ww");
                        parse = sdf.parse(key);
                        break;
                    case 1:
                        sdf = new SimpleDateFormat("yyyy-MM");
                        parse = sdf.parse(key);
                        break;
                    case 2:
                        sdf = new SimpleDateFormat("yyyy-MM");
                        List<QuaStatisticsVo.JmrsList> dptListList = entry.getValue().stream().sorted(Comparator.comparing(QuaStatisticsVo.JmrsList::getCreateTime)).collect(Collectors.toList());
                        Date createTime = dptListList.get(0).getCreateTime();
                        Calendar cal = Calendar.getInstance();
                        cal.setTime(createTime);
                        String str = "";
                        if ("1".equals(key)) {
                            str = "-01-01";
                        } else if ("2".equals(key)) {
                            str = "-04-01";
                        } else if ("3".equals(key)) {
                            str = "07-01";
                        } else if ("4".equals(key)) {
                            str = "10-01";
                        }
                        parse = sdf.parse(cal.get(Calendar.YEAR) + str);
                        break;
                    case 3:
                        sdf = new SimpleDateFormat("yyyy");
                        parse = sdf.parse(key);
                        break;
                    case 4:
                        sdf = new SimpleDateFormat("yyyy-MM-dd");
                        parse = sdf.parse(key);
                        break;
                    default:
                        break;
                }
                result.setTime(parse);
                result.setDate(sdf.format(parse));
                resultList.add(result);
            }
        }
        if (CollectionUtil.isNotEmpty(resultList)) {
            resultList = resultList.stream().sorted(Comparator.comparing(QuaStatisticsVo.JmrsResult::getTime)).collect(Collectors.toList());
            completionJmrsData(dto.getTimeType(), resultList, dto.getStartTime(), dto.getEndTime());
            resultList = resultList.stream().sorted(Comparator.comparing(QuaStatisticsVo.JmrsResult::getTime)).collect(Collectors.toList());
        }
        return resultList;
    }

    @Override
    public List<QuaStatisticsVo.DntResult> queryDntResult(QuaStatisticsDto.QueryZhibiao amsHomeDto) throws ParseException {
        List<QuaStatisticsVo.DntResult> resultList = new ArrayList<>();

        List<QuaStatisticsVo.DntList> dntList = quaStatisticsDao.selectDnt(amsHomeDto.getTimeType(), amsHomeDto.getStartTime(), amsHomeDto.getEndTime());
        if (dntList != null) {
            Map<String, List<QuaStatisticsVo.DntList>> map = dntList.stream().collect(Collectors.groupingBy(QuaStatisticsVo.DntList::getDateTime));
            for (Map.Entry<String, List<QuaStatisticsVo.DntList>> entry : map.entrySet()) {
                QuaStatisticsVo.DntResult dntResult = new QuaStatisticsVo.DntResult();

                for (QuaStatisticsVo.DntList list : entry.getValue()) {
                    Long dnt = list.getDnt();
                    if (dnt != 0) {
                        if (dnt > 60 * 60 * 1000) {
                            dntResult.setOther(dntResult.getOther() + 1);
                        }
                        if (45 * 60 * 1000 < dnt && dnt <= 60 * 60 * 1000) {
                            dntResult.setDy60(dntResult.getDy60() + 1);
                        }
                        if (45 * 60 * 1000 >= dnt) {
                            dntResult.setDy45(dntResult.getDy45() + 1);
                        }
                    }
                }

                SimpleDateFormat sdf;
                Date parse = new Date();
                String date = "";
                String key = entry.getKey();
                if (StrUtil.isEmpty(key)) {
                    continue;
                }
                switch (amsHomeDto.getTimeType()) {
                    case 0:
                        sdf = new SimpleDateFormat("yyyy-ww");
                        parse = sdf.parse(key);
                        date = sdf.format(parse);
                        break;
                    case 1:
                        sdf = new SimpleDateFormat("yyyy-MM");
                        parse = sdf.parse(key);
                        date = sdf.format(parse);
                        break;
                    case 2:
                        sdf = new SimpleDateFormat("yyyy-MM");
                        List<QuaStatisticsVo.DntList> list = entry.getValue().stream().sorted(Comparator.comparing(QuaStatisticsVo.DntList::getCreateTime)).collect(Collectors.toList());
                        Date createTime = list.get(0).getCreateTime();
                        Calendar cal = Calendar.getInstance();
                        cal.setTime(createTime);
                        String str = "";
                        if ("1".equals(key)) {
                            str = "-01-01";
                        } else if ("2".equals(key)) {
                            str = "-04-01";
                        } else if ("3".equals(key)) {
                            str = "07-01";
                        } else if ("4".equals(key)) {
                            str = "10-01";
                        }
                        parse = sdf.parse(cal.get(Calendar.YEAR) + str);
                        date = sdf.format(parse);
                        break;
                    case 3:
                        sdf = new SimpleDateFormat("yyyy");
                        parse = sdf.parse(key);
                        date = sdf.format(parse);
                        break;
                    case 4:
                        sdf = new SimpleDateFormat("yyyy-MM-dd");
                        parse = sdf.parse(key);
                        date = sdf.format(parse);
                        break;
                    default:
                        break;
                }
                dntResult.setTime(parse);
                dntResult.setDate(date);
                resultList.add(dntResult);
            }
        }
        if (CollectionUtil.isNotEmpty(resultList)) {
            resultList = resultList.stream().sorted(Comparator.comparing(QuaStatisticsVo.DntResult::getTime)).collect(Collectors.toList());
            resultList = completionDntData(amsHomeDto.getTimeType(), resultList, amsHomeDto.getStartTime(), amsHomeDto.getEndTime());
            resultList = resultList.stream().sorted(Comparator.comparing(QuaStatisticsVo.DntResult::getTime)).collect(Collectors.toList());
        }
        return resultList;
    }

    @Override
    public List<QuaStatisticsVo.BfzResultVo> querySichResult(QuaStatisticsDto.QueryPatient amsHomeDto) throws ParseException {
        List<QuaStatisticsVo.BfzResultVo> list = new ArrayList<>();
        List<QuaStatisticsVo.BfzResult> bfzResults = quaStatisticsDao.selectSich(amsHomeDto.getTimeType(), amsHomeDto.getStartTime(), amsHomeDto.getEndTime());
        if (CollectionUtil.isNotEmpty(bfzResults)) {
            Map<String, List<QuaStatisticsVo.BfzResult>> map = bfzResults.stream().collect(Collectors.groupingBy(QuaStatisticsVo.BfzResult::getDateTime));
            for (Map.Entry<String, List<QuaStatisticsVo.BfzResult>> entry : map.entrySet()) {
                QuaStatisticsVo.BfzResultVo bfzResultVo = new QuaStatisticsVo.BfzResultVo();
                //并发数量
                int num = 0;
                for (QuaStatisticsVo.BfzResult bfzResult : entry.getValue()) {
                    if (StrUtil.isNotEmpty(bfzResult.getAnswer()) && !"无".equals(bfzResult.getAnswer())) {
                        num += 1;
                    }
                }
                bfzResultVo.setBfNum(num);

                Date startDate = new Date();
                Date endDate = new Date();
                String date = "";
                //查询该时间段的患者数量
                String key = entry.getKey();
                if (StrUtil.isEmpty(key)) {
                    continue;
                }
                Calendar calendar = Calendar.getInstance();
                if (StrUtil.isNotEmpty(key)) {
                    switch (amsHomeDto.getTimeType()) {
                        case 0: {
                            String[] split = key.split("-");
                            calendar.set(Calendar.YEAR, Integer.parseInt(split[0]));
                            calendar.set(Calendar.WEEK_OF_YEAR, Integer.parseInt(split[1]));
                            startDate = calendar.getTime();
                            calendar.add(Calendar.WEEK_OF_YEAR, +1);
                            endDate = calendar.getTime();
                            PmsArchivesExample amsFirstAidExample = new PmsArchivesExample();
                            amsFirstAidExample.createCriteria().andCreateTimeBetween(startDate, endDate).andDelFlagEqualTo((byte) 0);
                            long l = pmsArchivesMapper.countByExample(amsFirstAidExample);
                            bfzResultVo.setTotal((int) l);
                            bfzResultVo.setDate(startDate);
                            bfzResultVo.setTime(calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH) + "-" + calendar.get(Calendar.DATE));
                            break;
                        }
                        case 1: {
                            PmsArchivesExample amsFirstAidExample = new PmsArchivesExample();
                            amsFirstAidExample.createCriteria().andDysjBetween(amsHomeDto.getStartTime(), amsHomeDto.getEndTime()).andDelFlagEqualTo((byte) 0);
                            long l = pmsArchivesMapper.countByExample(amsFirstAidExample);
                            bfzResultVo.setTotal((int) l);

                            String[] split = key.split("-");
                            calendar.set(Calendar.YEAR, Integer.parseInt(split[0]));
                            calendar.set(Calendar.MONTH, Integer.parseInt(split[1]));
                            startDate = new SimpleDateFormat("yyyy-MM").parse(split[0] + "-" + split[1]);
                            endDate = calendar.getTime();
                            bfzResultVo.setDate(startDate);
                            bfzResultVo.setTime(calendar.get(Calendar.YEAR) + "-" + calendar.get(Calendar.MONTH));
                            break;
                        }
                        case 2: {
                            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                            List<QuaStatisticsVo.BfzResult> results = entry.getValue().stream().sorted(Comparator.comparing(QuaStatisticsVo.BfzResult::getCreateTime)).collect(Collectors.toList());
                            Date createTime = results.get(0).getCreateTime();
                            Calendar cal = Calendar.getInstance();
                            cal.setTime(createTime);
                            String str = "";
                            if ("1".equals(key)) {
                                str = "-01-01";
                            } else if ("2".equals(key)) {
                                str = "-04-01";
                            } else if ("3".equals(key)) {
                                str = "-07-01";
                            } else if ("4".equals(key)) {
                                str = "-10-01";
                            }
                            startDate = sdf.parse(cal.get(Calendar.YEAR) + str);
                            cal.set(Calendar.MONTH, +3);
                            endDate = cal.getTime();
                            PmsArchivesExample amsFirstAidExample = new PmsArchivesExample();
                            amsFirstAidExample.createCriteria().andCreateTimeBetween(startDate, endDate).andDelFlagEqualTo((byte) 0);
                            long l = pmsArchivesMapper.countByExample(amsFirstAidExample);
                            bfzResultVo.setTotal((int) l);
                            bfzResultVo.setDate(startDate);
                            bfzResultVo.setTime(cal.get(Calendar.YEAR) + str);
                            break;
                        }
                        case 3: {
                            String[] split = key.split("-");
                            calendar.set(Calendar.YEAR, Integer.parseInt(split[0]));
                            startDate = calendar.getTime();
                            calendar.add(Calendar.YEAR, +1);
                            endDate = calendar.getTime();
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            startDate = simpleDateFormat.parse(key + "00:00:00");
                            endDate = simpleDateFormat.parse(key + "23:59:59");
                            PmsArchivesExample amsFirstAidExample = new PmsArchivesExample();
                            amsFirstAidExample.createCriteria().andCreateTimeBetween(startDate, endDate).andDelFlagEqualTo((byte) 0);
                            long l = pmsArchivesMapper.countByExample(amsFirstAidExample);
                            bfzResultVo.setTotal((int) l);
                            bfzResultVo.setTime(calendar.get(Calendar.YEAR) + "");
                            bfzResultVo.setDate(startDate);

                            break;
                        }
                        case 4: {
//                            String[] split = key.split("-");
//                            calendar.set(Calendar.YEAR, Integer.parseInt(split[0]));
//                            calendar.set(Calendar.MONTH, Integer.parseInt(split[1]));
//                            calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(split[2]));
//                            startDate = calendar.getTime();
//                            endDate = calendar.getTime();
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                            startDate = simpleDateFormat.parse(key + " 00:00:00");
                            endDate = simpleDateFormat.parse(key + " 23:59:59");
                            PmsArchivesExample amsFirstAidExample = new PmsArchivesExample();
                            amsFirstAidExample.createCriteria().andDysjBetween(startDate, endDate).andDelFlagEqualTo((byte) 0);
                            long l = pmsArchivesMapper.countByExample(amsFirstAidExample);
                            bfzResultVo.setTotal((int) l);
                            bfzResultVo.setDate(startDate);
                            bfzResultVo.setTime(DateUtil.format(startDate, "yyyy-MM-dd"));
                        }
                        default:
                            break;
                    }
                }
                if (bfzResultVo.getTotal() > 0 && bfzResultVo.getTotal() >= bfzResultVo.getBfNum()) {
                    bfzResultVo.setDecimal(new BigDecimal(bfzResultVo.getBfNum()).divide(new BigDecimal(bfzResultVo.getTotal()), 2));
                } else if (bfzResultVo.getTotal() < bfzResultVo.getBfNum()) {
                    bfzResultVo.setDecimal(new BigDecimal(1));
                } else {
                    bfzResultVo.setDecimal(new BigDecimal(0));
                }
                list.add(bfzResultVo);
            }
        }
        if (CollectionUtil.isNotEmpty(list)) {
            list = list.stream().sorted(Comparator.comparing(QuaStatisticsVo.BfzResultVo::getDate)).collect(Collectors.toList());
            list = completionSichData(amsHomeDto.getTimeType(), list, amsHomeDto.getStartTime(), amsHomeDto.getEndTime());
            list = list.stream().sorted(Comparator.comparing(QuaStatisticsVo.BfzResultVo::getDate)).collect(Collectors.toList());
        }
        list = list.stream().sorted(Comparator.comparing(QuaStatisticsVo.BfzResultVo::getDate)).collect(Collectors.toList());
        return list;
    }

    @Override
    public List<QuaStatisticsVo.DptResult> queryDptResult(QuaStatisticsDto.QueryZhibiao amsHomeDto) throws ParseException {
        List<QuaStatisticsVo.DptList> dptLists = quaStatisticsDao.selectDpt(amsHomeDto.getTimeType(), amsHomeDto.getStartTime(), amsHomeDto.getEndTime());
        List<QuaStatisticsVo.DptResult> resultList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(dptLists)) {
            Map<String, List<QuaStatisticsVo.DptList>> map = dptLists.stream().collect(Collectors.groupingBy(QuaStatisticsVo.DptList::getDateTime));
            for (Map.Entry<String, List<QuaStatisticsVo.DptList>> entry : map.entrySet()) {
                QuaStatisticsVo.DptResult dptList = new QuaStatisticsVo.DptResult();
                for (QuaStatisticsVo.DptList list : entry.getValue()) {
                    Long dptTime = list.getDptTime();
                    if (dptTime != 0) {
                        if (dptTime > 90 * 60 * 1000) {
                            dptList.setOther(dptList.getOther() + 1);
                        }
                        if (60 * 60 * 1000 < dptTime && dptTime <= 90 * 60 * 1000) {
                            dptList.setDy90(dptList.getDy60() + 1);
                        }
                        if (60 * 60 * 1000 >= dptTime) {
                            dptList.setDy60(dptList.getDy60() + 1);
                        }
                    }
                    SimpleDateFormat sdf;
                    Date parse = new Date();
                    String date = "";
                    String key = entry.getKey();
                    if (StrUtil.isEmpty(key)) {
                        continue;
                    }
                    switch (amsHomeDto.getTimeType()) {
                        case 0:
                            sdf = new SimpleDateFormat("yyyy-ww");
                            parse = sdf.parse(key);
                            date = sdf.format(parse);
                            break;
                        case 1:
                            sdf = new SimpleDateFormat("yyyy-MM");
                            parse = sdf.parse(key);
                            date = sdf.format(parse);
                            break;
                        case 2:
                            sdf = new SimpleDateFormat("yyyy-MM");
                            List<QuaStatisticsVo.DptList> dptListList = entry.getValue().stream().sorted(Comparator.comparing(QuaStatisticsVo.DptList::getCreateTime)).collect(Collectors.toList());
                            Date createTime = dptListList.get(0).getCreateTime();
                            Calendar cal = Calendar.getInstance();
                            cal.setTime(createTime);
                            String str = "";
                            if ("1".equals(key)) {
                                str = "-01-01";
                            } else if ("2".equals(key)) {
                                str = "-04-01";
                            } else if ("3".equals(key)) {
                                str = "07-01";
                            } else if ("4".equals(key)) {
                                str = "10-01";
                            }
                            parse = sdf.parse(cal.get(Calendar.YEAR) + str);
                            date = sdf.format(parse);
                            break;
                        case 3:
                            sdf = new SimpleDateFormat("yyyy");
                            parse = sdf.parse(key);
                            date = sdf.format(parse);
                            break;
                        case 4:
                            sdf = new SimpleDateFormat("yyyy-MM-dd");
                            parse = sdf.parse(key);
                            date = sdf.format(parse);
                            break;
                        default:
                            break;
                    }
                    dptList.setTime(parse);
                    dptList.setDate(date);
                }
                resultList.add(dptList);
            }
        }
        if (CollectionUtil.isNotEmpty(resultList)) {
            resultList = resultList.stream().sorted(Comparator.comparing(QuaStatisticsVo.DptResult::getTime)).collect(Collectors.toList());
            completionDptData(amsHomeDto.getTimeType(), resultList, amsHomeDto.getStartTime(), amsHomeDto.getEndTime());
            resultList = resultList.stream().sorted(Comparator.comparing(QuaStatisticsVo.DptResult::getTime)).collect(Collectors.toList());
        }
        return resultList;
    }

    @Override
    public List<QuaStatisticsVo.RptResult> queryRptResult(QuaStatisticsDto.QueryZhibiao amsHomeDto) throws ParseException {
        List<QuaStatisticsVo.RptList> rptLists = quaStatisticsDao.selectRpt(amsHomeDto.getTimeType(), amsHomeDto.getStartTime(), amsHomeDto.getEndTime());
        List<QuaStatisticsVo.RptResult> resultList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(rptLists)) {
            Map<String, List<QuaStatisticsVo.RptList>> map = rptLists.stream().collect(Collectors.groupingBy(QuaStatisticsVo.RptList::getDateTime));
            for (Map.Entry<String, List<QuaStatisticsVo.RptList>> entry : map.entrySet()) {
                QuaStatisticsVo.RptResult dptList = new QuaStatisticsVo.RptResult();
                for (QuaStatisticsVo.RptList list : entry.getValue()) {
                    Long dptTime = list.getRptTime();
                    if (dptTime != 0) {
                        if (dptTime > 90 * 60 * 1000) {
                            dptList.setOther(dptList.getOther() + 1);
                        }
                        if (60 * 60 * 1000 < dptTime && dptTime <= 90 * 60 * 1000) {
                            dptList.setDy90(dptList.getDy90() + 1);
                        }
                        if (60 * 60 * 1000 > dptTime) {
                            dptList.setDy60(dptList.getDy60() + 1);
                        }
                    }
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                    Date parse = new Date();
                    String key = entry.getKey();
                    if (StrUtil.isEmpty(key)) {
                        continue;
                    }
                    switch (amsHomeDto.getTimeType()) {
                        case 0:
                            sdf = new SimpleDateFormat("yyyy-ww");
                            parse = sdf.parse(key);
                            break;
                        case 1:
                            sdf = new SimpleDateFormat("yyyy-MM");
                            parse = sdf.parse(key);
                            break;
                        case 2:
                            sdf = new SimpleDateFormat("yyyy-MM");
                            List<QuaStatisticsVo.RptList> dptListList = entry.getValue().stream().sorted(Comparator.comparing(QuaStatisticsVo.RptList::getCreateTime)).collect(Collectors.toList());
                            Date createTime = dptListList.get(0).getCreateTime();
                            Calendar cal = Calendar.getInstance();
                            cal.setTime(createTime);
                            String str = "";
                            if ("1".equals(key)) {
                                str = "-01-01";
                            } else if ("2".equals(key)) {
                                str = "-04-01";
                            } else if ("3".equals(key)) {
                                str = "07-01";
                            } else if ("4".equals(key)) {
                                str = "10-01";
                            }
                            parse = sdf.parse(cal.get(Calendar.YEAR) + str);
                            break;
                        case 3:
                            sdf = new SimpleDateFormat("yyyy");
                            parse = sdf.parse(key);
                            break;
                        case 4:
                            sdf = new SimpleDateFormat("yyyy-MM-dd");
                            parse = sdf.parse(key);
                            break;
                        default:
                            break;
                    }
                    dptList.setTime(parse);
                    dptList.setDate(sdf.format(parse));
                }
                resultList.add(dptList);
            }
        }
        if (CollectionUtil.isNotEmpty(resultList)) {
            resultList = resultList.stream().sorted(Comparator.comparing(QuaStatisticsVo.RptResult::getTime)).collect(Collectors.toList());
            completionRptData(amsHomeDto.getTimeType(), resultList, amsHomeDto.getStartTime(), amsHomeDto.getEndTime());
            resultList = resultList.stream().sorted(Comparator.comparing(QuaStatisticsVo.RptResult::getTime)).collect(Collectors.toList());
        }
        return resultList;
    }

    @Override
    public List<QuaStatisticsVo.XgzlLvResult> queryXgzlLv(QuaStatisticsDto.QueryPatient amsHomeDto) throws ParseException {
        List<QuaStatisticsVo.XgzlLvResult> resultList = new ArrayList<>();
        List<QuaStatisticsVo.XgzlLvList> xgzlList = quaStatisticsDao.selectXgzlLv(amsHomeDto.getTimeType(), amsHomeDto.getStartTime(), amsHomeDto.getEndTime());
        if (CollectionUtil.isNotEmpty(xgzlList)) {
            Map<String, List<QuaStatisticsVo.XgzlLvList>> map = xgzlList.stream().collect(Collectors.groupingBy(QuaStatisticsVo.XgzlLvList::getDateTime));
            for (Map.Entry<String, List<QuaStatisticsVo.XgzlLvList>> entry : map.entrySet()) {
                QuaStatisticsVo.XgzlLvResult result = new QuaStatisticsVo.XgzlLvResult();
                int qjNmu = 0;
                int fqjNum = 0;
                for (QuaStatisticsVo.XgzlLvList rpt : entry.getValue()) {
                    if (rpt.isFlag()) {
                        result.setGailv(result.getGailv() + 1);
                    }
                    if (ZL_JRZT_TYPE.getKey().equals(rpt.getCode())) {
                        if ("1".equals(rpt.getAnswer())) {
                            qjNmu += 1;
                        } else {
                            fqjNum += 1;
                        }
                    }
                }
                result.setQjNmu(qjNmu);
                result.setFqjNum(fqjNum);
                result.setTotal(entry.getValue().stream().collect(Collectors.groupingBy(QuaStatisticsVo.XgzlLvList::getFirstAidId)).size());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                Date parse = new Date();
                String key = entry.getKey();
                if (StrUtil.isEmpty(key)) {
                    continue;
                }
                switch (amsHomeDto.getTimeType()) {
                    case 0:
                        sdf = new SimpleDateFormat("yyyy-ww");
                        parse = sdf.parse(key);
                        break;
                    case 1:
                        sdf = new SimpleDateFormat("yyyy-MM");
                        parse = sdf.parse(key);
                        break;
                    case 2:
                        sdf = new SimpleDateFormat("yyyy-MM");
                        List<QuaStatisticsVo.XgzlLvList> dptListList = entry.getValue().stream().sorted(Comparator.comparing(QuaStatisticsVo.XgzlLvList::getCreateTime)).collect(Collectors.toList());
                        Date createTime = dptListList.get(0).getCreateTime();
                        Calendar cal = Calendar.getInstance();
                        cal.setTime(createTime);
                        String str = "";
                        if ("1".equals(key)) {
                            str = "-01-01";
                        } else if ("2".equals(key)) {
                            str = "-04-01";
                        } else if ("3".equals(key)) {
                            str = "-07-01";
                        } else if ("4".equals(key)) {
                            str = "-10-01";
                        }
                        parse = sdf.parse(cal.get(Calendar.YEAR) + str);
                        break;
                    case 3:
                        sdf = new SimpleDateFormat("yyyy");
                        parse = sdf.parse(key);
                        break;
                    case 4:
                        sdf = new SimpleDateFormat("yyyy-MM-dd");
                        parse = sdf.parse(key);
                        break;
                    default:
                        break;
                }
                result.setTime(parse);
                result.setDate(sdf.format(parse));
                resultList.add(result);
            }
        }
        if (CollectionUtil.isNotEmpty(resultList)) {
            resultList = resultList.stream().sorted(Comparator.comparing(QuaStatisticsVo.XgzlLvResult::getTime)).collect(Collectors.toList());
            completionXgzlData(amsHomeDto.getTimeType(), resultList, amsHomeDto.getStartTime(), amsHomeDto.getEndTime());
            resultList = resultList.stream().sorted(Comparator.comparing(QuaStatisticsVo.XgzlLvResult::getTime)).collect(Collectors.toList());
        }
        return resultList;
    }

    @Override
    public List<QuaStatisticsVo.NcxResult> queryNcxLv(QuaStatisticsDto.QueryPatient amsHomeDto) throws ParseException {
        List<QuaStatisticsVo.NcxResult> resultList = new ArrayList<>();
        List<QuaStatisticsVo.NcxLvList> xgzlList = quaStatisticsDao.selectNcxLv(amsHomeDto.getTimeType(), amsHomeDto.getStartTime(), amsHomeDto.getEndTime());
        if (CollectionUtil.isNotEmpty(xgzlList)) {
            Map<String, List<QuaStatisticsVo.NcxLvList>> map = xgzlList.stream().collect(Collectors.groupingBy(QuaStatisticsVo.NcxLvList::getDateTime));
            for (Map.Entry<String, List<QuaStatisticsVo.NcxLvList>> entry : map.entrySet()) {
                QuaStatisticsVo.NcxResult result = new QuaStatisticsVo.NcxResult();
                for (QuaStatisticsVo.NcxLvList rpt : entry.getValue()) {
                    if (rpt.isFlag()) {
                        result.setGailv(result.getGailv() + 1);
                    }
                }
                result.setTotal(entry.getValue().stream().collect(Collectors.groupingBy(QuaStatisticsVo.NcxLvList::getFirstAidId)).size());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                Date parse = new Date();
                String key = entry.getKey();
                if (StrUtil.isEmpty(key)) {
                    continue;
                }
                switch (amsHomeDto.getTimeType()) {
                    case 0:
                        sdf = new SimpleDateFormat("yyyy-ww");
                        parse = sdf.parse(key);
                        break;
                    case 1:
                        sdf = new SimpleDateFormat("yyyy-MM");
                        parse = sdf.parse(key);
                        break;
                    case 2:
                        sdf = new SimpleDateFormat("yyyy-MM");
                        List<QuaStatisticsVo.NcxLvList> dptListList = entry.getValue().stream().sorted(Comparator.comparing(QuaStatisticsVo.NcxLvList::getCreateTime)).collect(Collectors.toList());
                        Date createTime = dptListList.get(0).getCreateTime();
                        Calendar cal = Calendar.getInstance();
                        cal.setTime(createTime);
                        String str = "";
                        if ("1".equals(key)) {
                            str = "-01-01";
                        } else if ("2".equals(key)) {
                            str = "-04-01";
                        } else if ("3".equals(key)) {
                            str = "-07-01";
                        } else if ("4".equals(key)) {
                            str = "-10-01";
                        }
                        parse = sdf.parse(cal.get(Calendar.YEAR) + str);
                        break;
                    case 3:
                        sdf = new SimpleDateFormat("yyyy");
                        parse = sdf.parse(key);
                        break;
                    case 4:
                        sdf = new SimpleDateFormat("yyyy-MM-dd");
                        parse = sdf.parse(key);
                        break;
                    default:
                        break;
                }
                result.setTime(parse);
                result.setDate(sdf.format(parse));
                resultList.add(result);
            }
        }
        if (CollectionUtil.isNotEmpty(resultList)) {
            resultList = resultList.stream().sorted(Comparator.comparing(QuaStatisticsVo.NcxResult::getTime)).collect(Collectors.toList());
            completionNcxData(amsHomeDto.getTimeType(), resultList, amsHomeDto.getStartTime(), amsHomeDto.getEndTime());
            resultList = resultList.stream().sorted(Comparator.comparing(QuaStatisticsVo.NcxResult::getTime)).collect(Collectors.toList());
        }
        return resultList;
    }

    @Override
    public QuaStatisticsVo.QsfxResult queryQsfx(QuaStatisticsDto.Query dto) throws ParseException {
        List<QuaStatisticsVo.QsfxResult.Child> QsfxResultChildList = new ArrayList<>();
        QuaStatisticsVo.QsfxResult qsfxResult = new QuaStatisticsVo.QsfxResult();
        List<QuaStatisticsVo.NcxLvList> xgzlList = quaStatisticsDao.selectNcxLv(null, dto.getStartTime(), dto.getEndTime());
        if (CollectionUtil.isEmpty(xgzlList)) {
            return null;
        }
        //静脉溶栓数据
        int jmrsNum = 0;
        //首诊到病发14天内住院脑梗死患者例数
        int ngs14Num = 0;
        List<QuaStatisticsVo.QsfxList> qsfxLists = BeanUtil.copyToList(xgzlList, QuaStatisticsVo.QsfxList.class);
        Map<Long, List<QuaStatisticsVo.QsfxList>> collect = qsfxLists.stream().collect(Collectors.groupingBy(QuaStatisticsVo.QsfxList::getFirstAidId));
        for (Map.Entry<Long, List<QuaStatisticsVo.QsfxList>> entry : collect.entrySet()) {
            List<QuaStatisticsVo.QsfxList> value = entry.getValue();
            Long key = entry.getKey();
            for (QuaStatisticsVo.QsfxList qsfxList : value) {
                //急性脑梗死静脉溶栓率 = 该月静脉溶栓例数 / 首诊到病发14天内住院脑梗死患者例数
                if (StrUtil.isNotEmpty(qsfxList.getCode()) && JMRS_TIME.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer())) {
                    jmrsNum += 1;
                }
            }
            Map<String, List<QuaStatisticsVo.QsfxList>> map = value.stream().collect(Collectors.groupingBy(QuaStatisticsVo.QsfxList::getCode));
            List<QuaStatisticsVo.QsfxList> qsfxLists1 = map.get(ZL_JBXX_ZDBM.getKey());
            if (CollectionUtil.isNotEmpty(qsfxLists1)) {
                if ("I63".equals(qsfxLists1.get(0).getAnswer())) {
                    ngs14Num += 1;
                }
            }
        }

        QuaStatisticsVo.QsfxResult.Child child = new QuaStatisticsVo.QsfxResult.Child();
        if (ngs14Num != 0) {
            child.setBysj(new BigDecimal(jmrsNum).divide(new BigDecimal(ngs14Num), 2, RoundingMode.HALF_UP));
        }
        child.setType((byte) 0);
        QsfxResultChildList.add(child);
        qsfxResult.setChildList(QsfxResultChildList);

        //查询DNT中位数
        QuaStatisticsDto.QueryZhibiao queryZhibiao = new QuaStatisticsDto.QueryZhibiao();
        queryZhibiao.setType(0);
        queryZhibiao.setStartTime(dto.getStartTime());
        queryZhibiao.setEndTime(dto.getEndTime());
        List<QuaStatisticsVo.DntResult> resultList = queryDnt(queryZhibiao);
        QuaStatisticsVo.QsfxResult.Child child1 = new QuaStatisticsVo.QsfxResult.Child();
        if (CollectionUtil.isNotEmpty(resultList)) {
            QuaStatisticsVo.DntResult dntResult = resultList.get(0);
            child1.setType((byte) 1);
            child1.setBysj(dntResult.getMedian());
        }
        QsfxResultChildList.add(child1);

        //DNT<60min达标率
        QuaStatisticsVo.QsfxResult.Child child2 = new QuaStatisticsVo.QsfxResult.Child();
        child2.setType((byte) 2);
        QsfxResultChildList.add(child2);

        QuaStatisticsVo.QsfxResult.Child child3 = new QuaStatisticsVo.QsfxResult.Child();
        child3.setType((byte) 3);
        QsfxResultChildList.add(child3);

        QuaStatisticsVo.QsfxResult.Child child4 = new QuaStatisticsVo.QsfxResult.Child();
        child4.setType((byte) 4);
        QsfxResultChildList.add(child4);


        QuaStatisticsVo.QsfxResult.Child child5 = new QuaStatisticsVo.QsfxResult.Child();
        child5.setType((byte) 5);
        QsfxResultChildList.add(child5);


        QuaStatisticsVo.QsfxResult.Child child6 = new QuaStatisticsVo.QsfxResult.Child();
        child6.setType((byte) 6);
        QsfxResultChildList.add(child6);


        QuaStatisticsVo.QsfxResult.Child child7 = new QuaStatisticsVo.QsfxResult.Child();
        child7.setType((byte) 7);
        QsfxResultChildList.add(child7);

        QuaStatisticsVo.QsfxResult.Child child8 = new QuaStatisticsVo.QsfxResult.Child();
        child8.setType((byte) 8);
        QsfxResultChildList.add(child8);

        QuaStatisticsVo.QsfxResult.Child child9 = new QuaStatisticsVo.QsfxResult.Child();
        child9.setType((byte) 9);
        QsfxResultChildList.add(child9);
        return qsfxResult;
    }

    @Override
    public BigDecimal queryjxncxhzyzcdpg(List<QuaStatisticsVo.NcxLvList> list, QuaStatisticsDto.QueryPatient amsHomeDto) {
        //急性脑出血患者严重程度评估率 = 入院6 h内行脑出血评分评估的急性脑出血患者数 / 同期住院脑出血患者总数
        if (CollectionUtil.isEmpty(list)) {
            return null;
        }
        Map<Long, List<QuaStatisticsVo.NcxLvList>> collect = list.stream().collect(Collectors.groupingBy(QuaStatisticsVo.NcxLvList::getFirstAidId));
        for (Map.Entry<Long, List<QuaStatisticsVo.NcxLvList>> entry : collect.entrySet()) {
            List<QuaStatisticsVo.NcxLvList> value = entry.getValue();
            Long key = entry.getKey();
            for (QuaStatisticsVo.NcxLvList qsfxList : value) {
                if (StrUtil.isNotEmpty(qsfxList.getCode()) && qsfxList.getCode().equals("")) {

                }
            }
        }

        return null;
    }

    @Override
    public BigDecimal queryJxncxhzkfzll(List<QuaStatisticsVo.NcxLvList> list, QuaStatisticsDto.QueryPatient amsHomeDto) {
        //急性脑出血患者康复治疗率 = 进行康复治疗的急性脑出血患者数 / 同期住院的急性脑出血患者总数
        //脑出血判断依据： 治疗类型是脑出血 或者  脑出血是否在院
        Map<Long, List<QuaStatisticsVo.NcxLvList>> map = list.stream().collect(Collectors.groupingBy(QuaStatisticsVo.NcxLvList::getFirstAidId));
        //同期住院的急性脑出血患者总数
        int ncxhzs = 0;
        //进行康复治疗的急性脑出血患者数
        int kfzlhzs = 0;
        for (Map.Entry<Long, List<QuaStatisticsVo.NcxLvList>> entry : map.entrySet()) {
            List<QuaStatisticsVo.NcxLvList> value = entry.getValue();
            Long key = entry.getKey();
            boolean flag = false;
            for (QuaStatisticsVo.NcxLvList qsfxList : value) {
                //判断脑出血
                if (!flag) {
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZLLX_TYPE.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "3".equals(qsfxList.getAnswer()))
                            || (ZL_FBQK_NCX.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        ncxhzs += 1;
                        flag = true;
                    }
                } else {
                    //如是脑出血，则判断是否进行康复治疗
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZL_KFZL_IS.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        kfzlhzs += 1;
                        break;
                    }
                }
            }
        }
        if (ncxhzs == 0) {
            return new BigDecimal(0);
        }
        return new BigDecimal(kfzlhzs).divide(new BigDecimal(ncxhzs), RoundingMode.CEILING);
    }

    @Override
    public BigDecimal queryjxncxhzfbjyywzll(List<QuaStatisticsVo.NcxLvList> list, QuaStatisticsDto.QueryPatient amsHomeDto) {
        //急性脑出血患者发病6 h内降压药物治疗率 = 发病6h内给予降压药物治疗的急性脑出血患者数 / 同期发病6 h内到院的急性脑出血患者总数
        Map<Long, List<QuaStatisticsVo.NcxLvList>> map = list.stream().collect(Collectors.groupingBy(QuaStatisticsVo.NcxLvList::getFirstAidId));
        //同期发病6 h内到院的急性脑出血患者总数
        int ncxhzs = 0;
        //发病6h内给予降压药物治疗的急性脑出血患者数
        int jyyhzs = 0;
        for (Map.Entry<Long, List<QuaStatisticsVo.NcxLvList>> entry : map.entrySet()) {
            List<QuaStatisticsVo.NcxLvList> value = entry.getValue();
            Long key = entry.getKey();
            boolean flag = false;
            for (QuaStatisticsVo.NcxLvList qsfxList : value) {
                //判断脑出血
                if (!flag) {
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZLLX_TYPE.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "3".equals(qsfxList.getAnswer()))
                            || (ZL_FBQK_NCX.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        ncxhzs += 1;
                        flag = true;
                    }
                } else {
                    //如是脑出血，则判断是否进行康复治疗
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZL_KFZL_IS.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        jyyhzs += 1;
                        break;
                    }
                }
            }
        }
        if (ncxhzs == 0) {
            return new BigDecimal(0);
        }
        return new BigDecimal(jyyhzs).divide(new BigDecimal(ncxhzs), RoundingMode.CEILING);
    }

    @Override
    public BigDecimal queryncxhzzyqzbyscl(List<QuaStatisticsVo.NcxLvList> list, QuaStatisticsDto.QueryPatient amsHomeDto) {
        //脑出血患者住院7 d内潜在病因筛查率 = 住院7 d内完善潜在病因筛查脑出血患者数 / 同期需筛查的住院脑出血患者总数
        Map<Long, List<QuaStatisticsVo.NcxLvList>> map = list.stream().collect(Collectors.groupingBy(QuaStatisticsVo.NcxLvList::getFirstAidId));
        //同期发病6 h内到院的急性脑出血患者总数
        int ncxhzs = 0;
        //发病6h内给予降压药物治疗的急性脑出血患者数
        int jyyhzs = 0;
        for (Map.Entry<Long, List<QuaStatisticsVo.NcxLvList>> entry : map.entrySet()) {
            List<QuaStatisticsVo.NcxLvList> value = entry.getValue();
            Long key = entry.getKey();
            boolean flag = false;
            for (QuaStatisticsVo.NcxLvList qsfxList : value) {
                //判断脑出血
                if (!flag) {
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZLLX_TYPE.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "3".equals(qsfxList.getAnswer()))
                            || (ZL_FBQK_NCX.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        ncxhzs += 1;
                        flag = true;
                    }
                } else {
                    //如是脑出血，则判断是否进行康复治疗
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZL_KFZL_IS.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        jyyhzs += 1;
                        break;
                    }
                }
            }
        }
        if (ncxhzs == 0) {
            return new BigDecimal(0);
        }
        return new BigDecimal(jyyhzs).divide(new BigDecimal(ncxhzs), RoundingMode.CEILING);
    }

    @Override
    public BigDecimal queryKnxgncxhzknnzzll(List<QuaStatisticsVo.NcxLvList> list, QuaStatisticsDto.QueryPatient amsHomeDto) {
        //抗凝相关脑出血患者抗凝逆转治疗率 = 抗凝相关脑出血患者使用药物逆转治疗人数 / 同期到院的抗凝相关脑出血患者总数
        Map<Long, List<QuaStatisticsVo.NcxLvList>> map = list.stream().collect(Collectors.groupingBy(QuaStatisticsVo.NcxLvList::getFirstAidId));
        //同期发病6 h内到院的急性脑出血患者总数
        int ncxhzs = 0;
        //发病6h内给予降压药物治疗的急性脑出血患者数
        int jyyhzs = 0;
        for (Map.Entry<Long, List<QuaStatisticsVo.NcxLvList>> entry : map.entrySet()) {
            List<QuaStatisticsVo.NcxLvList> value = entry.getValue();
            Long key = entry.getKey();
            boolean flag = false;
            for (QuaStatisticsVo.NcxLvList qsfxList : value) {
                //判断脑出血
                if (!flag) {
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZLLX_TYPE.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "3".equals(qsfxList.getAnswer()))
                            || (ZL_FBQK_NCX.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        ncxhzs += 1;
                        flag = true;
                    }
                } else {
                    //如是脑出血，则判断是否进行康复治疗
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZL_KFZL_IS.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        jyyhzs += 1;
                        break;
                    }
                }
            }
        }
        if (ncxhzs == 0) {
            return new BigDecimal(0);
        }
        return new BigDecimal(jyyhzs).divide(new BigDecimal(ncxhzs), RoundingMode.CEILING);
    }

    @Override
    public BigDecimal queryjxncxhzczxgfyscl(List<QuaStatisticsVo.NcxLvList> list, QuaStatisticsDto.QueryPatient amsHomeDto) {
        //急性脑出血患者卒中相关肺炎筛查率 = 进行卒中相关肺炎筛查的急性脑出血患者数 / 同期住院的急性脑出血患者总数
        Map<Long, List<QuaStatisticsVo.NcxLvList>> map = list.stream().collect(Collectors.groupingBy(QuaStatisticsVo.NcxLvList::getFirstAidId));
        //同期发病6 h内到院的急性脑出血患者总数
        int ncxhzs = 0;
        //发病6h内给予降压药物治疗的急性脑出血患者数
        int jyyhzs = 0;
        for (Map.Entry<Long, List<QuaStatisticsVo.NcxLvList>> entry : map.entrySet()) {
            List<QuaStatisticsVo.NcxLvList> value = entry.getValue();
            Long key = entry.getKey();
            boolean flag = false;
            for (QuaStatisticsVo.NcxLvList qsfxList : value) {
                //判断脑出血
                if (!flag) {
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZLLX_TYPE.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "3".equals(qsfxList.getAnswer()))
                            || (ZL_FBQK_NCX.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        ncxhzs += 1;
                        flag = true;
                    }
                } else {
                    //如是脑出血，则判断是否进行康复治疗
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZL_KFZL_IS.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        jyyhzs += 1;
                        break;
                    }
                }
            }
        }
        if (ncxhzs == 0) {
            return new BigDecimal(0);
        }
        return new BigDecimal(jyyhzs).divide(new BigDecimal(ncxhzs), RoundingMode.CEILING);
    }

    @Override
    public BigDecimal queryNcxhzfctlctjcl(List<QuaStatisticsVo.NcxLvList> list, QuaStatisticsDto.QueryPatient amsHomeDto) {
        //脑出血患者发病24 h内复查头颅CT等影像学检查率 = 发病24 h内复查头颅CT等影像学的脑出血患者数 / 同期发病6 h内到院的脑出血患者总数
        Map<Long, List<QuaStatisticsVo.NcxLvList>> map = list.stream().collect(Collectors.groupingBy(QuaStatisticsVo.NcxLvList::getFirstAidId));
        //同期发病6 h内到院的急性脑出血患者总数
        int ncxhzs = 0;
        //发病6h内给予降压药物治疗的急性脑出血患者数
        int jyyhzs = 0;
        for (Map.Entry<Long, List<QuaStatisticsVo.NcxLvList>> entry : map.entrySet()) {
            List<QuaStatisticsVo.NcxLvList> value = entry.getValue();
            Long key = entry.getKey();
            boolean flag = false;
            for (QuaStatisticsVo.NcxLvList qsfxList : value) {
                //判断脑出血
                if (!flag) {
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZLLX_TYPE.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "3".equals(qsfxList.getAnswer()))
                            || (ZL_FBQK_NCX.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        ncxhzs += 1;
                        flag = true;
                    }
                } else {
                    //如是脑出血，则判断是否进行康复治疗
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZL_KFZL_IS.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        jyyhzs += 1;
                        break;
                    }
                }
            }
        }
        if (ncxhzs == 0) {
            return new BigDecimal(0);
        }
        return new BigDecimal(jyyhzs).divide(new BigDecimal(ncxhzs), RoundingMode.CEILING);
    }

    @Override
    public BigDecimal queryHbgxyjyywzll(List<QuaStatisticsVo.NcxLvList> list, QuaStatisticsDto.QueryPatient amsHomeDto) {
        // 合并高血压的脑出血患者出院时降压药物治疗率 = 出院时给予降压药物治疗的合并高血压脑出血患者数 / 同期住院的合并高血压脑出血患者总数
        Map<Long, List<QuaStatisticsVo.NcxLvList>> map = list.stream().collect(Collectors.groupingBy(QuaStatisticsVo.NcxLvList::getFirstAidId));
        //同期发病6 h内到院的急性脑出血患者总数
        int ncxhzs = 0;
        //发病6h内给予降压药物治疗的急性脑出血患者数
        int jyyhzs = 0;
        for (Map.Entry<Long, List<QuaStatisticsVo.NcxLvList>> entry : map.entrySet()) {
            List<QuaStatisticsVo.NcxLvList> value = entry.getValue();
            Long key = entry.getKey();
            boolean flag = false;
            for (QuaStatisticsVo.NcxLvList qsfxList : value) {
                //判断脑出血
                if (!flag) {
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZLLX_TYPE.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "3".equals(qsfxList.getAnswer()))
                            || (ZL_FBQK_NCX.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        ncxhzs += 1;
                        flag = true;
                    }
                } else {
                    //如是脑出血，则判断是否进行康复治疗
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZL_KFZL_IS.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        jyyhzs += 1;
                        break;
                    }
                }
            }
        }
        if (ncxhzs == 0) {
            return new BigDecimal(0);
        }
        return new BigDecimal(jyyhzs).divide(new BigDecimal(ncxhzs), RoundingMode.CEILING);
    }

    @Override
    public BigDecimal queryNcxsjmxsyfl(List<QuaStatisticsVo.NcxLvList> list, QuaStatisticsDto.QueryPatient amsHomeDto) {
        //脑出血患者入院48 h内深静脉血栓预防率 = 脑出血患者人院48 h内给予DVT预防措施的人数 / 同期住院脑出血患者总数
        Map<Long, List<QuaStatisticsVo.NcxLvList>> map = list.stream().collect(Collectors.groupingBy(QuaStatisticsVo.NcxLvList::getFirstAidId));
        //同期发病6 h内到院的急性脑出血患者总数
        int ncxhzs = 0;
        //发病6h内给予降压药物治疗的急性脑出血患者数
        int jyyhzs = 0;
        for (Map.Entry<Long, List<QuaStatisticsVo.NcxLvList>> entry : map.entrySet()) {
            List<QuaStatisticsVo.NcxLvList> value = entry.getValue();
            Long key = entry.getKey();
            boolean flag = false;
            for (QuaStatisticsVo.NcxLvList qsfxList : value) {
                //判断脑出血
                if (!flag) {
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZLLX_TYPE.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "3".equals(qsfxList.getAnswer()))
                            || (ZL_FBQK_NCX.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        ncxhzs += 1;
                        flag = true;
                    }
                } else {
                    //如是脑出血，则判断是否进行康复治疗
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZL_KFZL_IS.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        jyyhzs += 1;
                        break;
                    }
                }
            }
        }
        if (ncxhzs == 0) {
            return new BigDecimal(0);
        }
        return new BigDecimal(jyyhzs).divide(new BigDecimal(ncxhzs), RoundingMode.CEILING);
    }

    @Override
    public BigDecimal queryJxncxzybsl(List<QuaStatisticsVo.NcxLvList> list, QuaStatisticsDto.QueryPatient amsHomeDto) {
        //急性脑出血患者住院病死率 = 住院期间死亡的急性脑出血患者数 / 同期住院的急性脑出血患者总数
        Map<Long, List<QuaStatisticsVo.NcxLvList>> map = list.stream().collect(Collectors.groupingBy(QuaStatisticsVo.NcxLvList::getFirstAidId));
        //同期发病6 h内到院的急性脑出血患者总数
        int ncxhzs = 0;
        //发病6h内给予降压药物治疗的急性脑出血患者数
        int jyyhzs = 0;
        for (Map.Entry<Long, List<QuaStatisticsVo.NcxLvList>> entry : map.entrySet()) {
            List<QuaStatisticsVo.NcxLvList> value = entry.getValue();
            Long key = entry.getKey();
            boolean flag = false;
            for (QuaStatisticsVo.NcxLvList qsfxList : value) {
                //判断脑出血
                if (!flag) {
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZLLX_TYPE.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "3".equals(qsfxList.getAnswer()))
                            || (ZL_FBQK_NCX.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        ncxhzs += 1;
                        flag = true;
                    }
                } else {
                    //如是脑出血，则判断是否进行康复治疗
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZL_KFZL_IS.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        jyyhzs += 1;
                        break;
                    }
                }
            }
        }
        if (ncxhzs == 0) {
            return new BigDecimal(0);
        }
        return new BigDecimal(jyyhzs).divide(new BigDecimal(ncxhzs), RoundingMode.CEILING);
    }

    @Override
    public BigDecimal queryNcxhztygnscl(List<QuaStatisticsVo.NcxLvList> list, QuaStatisticsDto.QueryPatient amsHomeDto) {
        //脑出血患者吞咽功能筛查率  = 进食、水前进行吞咽功能筛查的住院脑出血患者数 / 同期住院脑出血患者总数
        Map<Long, List<QuaStatisticsVo.NcxLvList>> map = list.stream().collect(Collectors.groupingBy(QuaStatisticsVo.NcxLvList::getFirstAidId));
        //同期发病6 h内到院的急性脑出血患者总数
        int ncxhzs = 0;
        //发病6h内给予降压药物治疗的急性脑出血患者数
        int jyyhzs = 0;
        for (Map.Entry<Long, List<QuaStatisticsVo.NcxLvList>> entry : map.entrySet()) {
            List<QuaStatisticsVo.NcxLvList> value = entry.getValue();
            Long key = entry.getKey();
            boolean flag = false;
            for (QuaStatisticsVo.NcxLvList qsfxList : value) {
                //判断脑出血
                if (!flag) {
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZLLX_TYPE.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "3".equals(qsfxList.getAnswer()))
                            || (ZL_FBQK_NCX.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        ncxhzs += 1;
                        flag = true;
                    }
                } else {
                    //如是脑出血，则判断是否进行康复治疗
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZL_KFZL_IS.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        jyyhzs += 1;
                        break;
                    }
                }
            }
        }
        if (ncxhzs == 0) {
            return new BigDecimal(0);
        }
        return new BigDecimal(jyyhzs).divide(new BigDecimal(ncxhzs), RoundingMode.CEILING);
    }

    @Override
    public QuaStatisticsVo.NcxLvResult queryNcx(QuaStatisticsDto.QueryPatient amsHomeDto) {
        QuaStatisticsVo.NcxLvResult ncxLvResult = new QuaStatisticsVo.NcxLvResult();
        List<QuaStatisticsVo.NcxLvList> xgzlList = quaStatisticsDao.selectNcxLv(amsHomeDto.getTimeType(), amsHomeDto.getStartTime(), amsHomeDto.getEndTime());
        if (CollectionUtil.isEmpty(xgzlList)) {
            return ncxLvResult;
        }
        //计算指标
        ncxLvResult.setJxncxkfzll(queryJxncxhzkfzll(xgzlList, amsHomeDto));
        ncxLvResult.setKnnzzll(queryKnxgncxhzknnzzll(xgzlList, amsHomeDto));
        ncxLvResult.setZybsl(queryJxncxzybsl(xgzlList, amsHomeDto));
        ncxLvResult.setYwzll(queryjxncxhzfbjyywzll(xgzlList, amsHomeDto));
        ncxLvResult.setTygnscl(queryNcxhztygnscl(xgzlList, amsHomeDto));
        ncxLvResult.setSjmxsyfl(queryNcxsjmxsyfl(xgzlList, amsHomeDto));
        ncxLvResult.setJyywzll(queryHbgxyjyywzll(xgzlList, amsHomeDto));
        ncxLvResult.setCtjcl(queryNcxhzfctlctjcl(xgzlList, amsHomeDto));
        ncxLvResult.setQzbyscl(queryncxhzzyqzbyscl(xgzlList, amsHomeDto));
        ncxLvResult.setFyscl(queryjxncxhzczxgfyscl(xgzlList, amsHomeDto));
        ncxLvResult.setJxncxpgl(queryjxncxhzyzcdpg(xgzlList, amsHomeDto));
        return ncxLvResult;
    }

    @Override
    public QuaStatisticsVo.NgsReslut queryNgs(QuaStatisticsDto.QueryPatient amsHomeDto) {
        QuaStatisticsVo.NgsReslut ngsReslut = new QuaStatisticsVo.NgsReslut();
        List<QuaStatisticsVo.NcxLvList> xgzlList = quaStatisticsDao.selectNcxLv(amsHomeDto.getTimeType(), amsHomeDto.getStartTime(), amsHomeDto.getEndTime());
        ngsReslut.setBqpgResult(queryBqpgResult(xgzlList, amsHomeDto));
        ngsReslut.setGfhzlyyfbfz(queryGfhzlyyfbfz(xgzlList, amsHomeDto));
        ngsReslut.setJjzb(queryJjzb(xgzlList, amsHomeDto));
        ngsReslut.setEjyf(queryEjyf(xgzlList, amsHomeDto));
        ngsReslut.setJxnxgzgzzlnlResult(queryJxnxgzgzzlnlResult(xgzlList, amsHomeDto));
        return ngsReslut;
    }

    @Override
    public List<QuaStatisticsVo.OntReslut> queryOnt(QuaStatisticsDto.QueryZhibiao amsHomeDto) {
        List<QuaStatisticsVo.OntReslut> list = new ArrayList<>();
        List<QuaStatisticsVo.OntList> dntList = quaStatisticsDao.selectOnt(amsHomeDto.getTimeType(), amsHomeDto.getStartTime(), amsHomeDto.getEndTime());
        if (CollectionUtil.isEmpty(dntList)) {
            return null;
        }
        Map<String, List<QuaStatisticsVo.OntList>> map = dntList.stream().collect(Collectors.groupingBy(QuaStatisticsVo.OntList::getDateTime));
        for (Map.Entry<String, List<QuaStatisticsVo.OntList>> entry : map.entrySet()) {
            if (CollectionUtil.isEmpty(entry.getValue())) {
                continue;
            }
            QuaStatisticsVo.OntReslut dntResult = new QuaStatisticsVo.OntReslut();
            //统计每天/月/年的 DNT平均时间
            long sum = entry.getValue().stream().mapToLong(QuaStatisticsVo.OntList::getOntTime).sum() / entry.getValue().size();
            dntResult.setXTime(entry.getKey());
            dntResult.setAverage(new BigDecimal(sum));
            //获取中位数
            BigDecimal median = new BigDecimal(0);
            List<Long> dntTimeList = entry.getValue().stream().map(QuaStatisticsVo.OntList::getOntTime).collect(Collectors.toList());
            Collections.sort(dntTimeList);
            int size = dntTimeList.size();
            if (size % 2 == 1) {
                median = new BigDecimal(dntTimeList.get((size - 1))).divide(new BigDecimal(2), 2, RoundingMode.HALF_UP);
            } else {
                //加0.0是为了把int转成double类型，否则除以2会算错
                median = new BigDecimal(dntTimeList.get(size / 2 - 1) + dntTimeList.get(size / 2)).divide(new BigDecimal(2), 2, RoundingMode.HALF_UP);
            }
            dntResult.setMedian(median);
            list.add(dntResult);
        }
        return list.stream().sorted(Comparator.comparing(QuaStatisticsVo.OntReslut::getXTime)).collect(Collectors.toList());
    }

    @Override
    public List<QuaStatisticsVo.OntReslut> queryDpt(QuaStatisticsDto.QueryZhibiao amsHomeDto) {
        List<QuaStatisticsVo.OntReslut> list = new ArrayList<>();
        List<QuaStatisticsVo.OptList> dntList = quaStatisticsDao.selectOpt(amsHomeDto.getTimeType(), amsHomeDto.getStartTime(), amsHomeDto.getEndTime());
        if (CollectionUtil.isEmpty(dntList)) {
            return null;
        }
        Map<String, List<QuaStatisticsVo.OptList>> map = dntList.stream().collect(Collectors.groupingBy(QuaStatisticsVo.OptList::getDateTime));
        for (Map.Entry<String, List<QuaStatisticsVo.OptList>> entry : map.entrySet()) {
            if (CollectionUtil.isEmpty(entry.getValue())) {
                continue;
            }
            QuaStatisticsVo.OntReslut dntResult = new QuaStatisticsVo.OntReslut();
            //统计每天/月/年的 DNT平均时间
            long sum = entry.getValue().stream().mapToLong(QuaStatisticsVo.OptList::getOptTime).sum() / entry.getValue().size();
            dntResult.setXTime(entry.getKey());
            dntResult.setAverage(new BigDecimal(sum));
            //获取中位数
            BigDecimal median = new BigDecimal(0);
            List<Long> dntTimeList = entry.getValue().stream().map(QuaStatisticsVo.OptList::getOptTime).collect(Collectors.toList());
            Collections.sort(dntTimeList);
            int size = dntTimeList.size();
            if (size % 2 == 1) {
                median = new BigDecimal(dntTimeList.get((size - 1))).divide(new BigDecimal(2), 2, RoundingMode.HALF_UP);
            } else {
                //加0.0是为了把int转成double类型，否则除以2会算错
                median = new BigDecimal(dntTimeList.get(size / 2 - 1) + dntTimeList.get(size / 2)).divide(new BigDecimal(2), 2, RoundingMode.HALF_UP);
            }
            dntResult.setMedian(median);
            list.add(dntResult);
        }
        return list.stream().sorted(Comparator.comparing(QuaStatisticsVo.OntReslut::getXTime)).collect(Collectors.toList());
    }

    @Override
    public List<QuaStatisticsVo.OptReslut> queryOpt(QuaStatisticsDto.QueryZhibiao amsHomeDto) {
        List<QuaStatisticsVo.OptReslut> list = new ArrayList<>();
        List<QuaStatisticsVo.OptList> dntList = quaStatisticsDao.selectOpt(amsHomeDto.getTimeType(), amsHomeDto.getStartTime(), amsHomeDto.getEndTime());
        if (CollectionUtil.isEmpty(dntList)) {
            return null;
        }
        Map<String, List<QuaStatisticsVo.OptList>> map = dntList.stream().collect(Collectors.groupingBy(QuaStatisticsVo.OptList::getDateTime));
        for (Map.Entry<String, List<QuaStatisticsVo.OptList>> entry : map.entrySet()) {
            if (CollectionUtil.isEmpty(entry.getValue())) {
                continue;
            }
            QuaStatisticsVo.OptReslut dntResult = new QuaStatisticsVo.OptReslut();
            //统计每天/月/年的 DNT平均时间
            long sum = entry.getValue().stream().mapToLong(QuaStatisticsVo.OptList::getOptTime).sum() / entry.getValue().size();
            dntResult.setXTime(entry.getKey());
            dntResult.setAverage(new BigDecimal(sum));
            //获取中位数
            BigDecimal median = new BigDecimal(0);
            List<Long> dntTimeList = entry.getValue().stream().map(QuaStatisticsVo.OptList::getOptTime).collect(Collectors.toList());
            Collections.sort(dntTimeList);
            int size = dntTimeList.size();
            if (size % 2 == 1) {
                median = new BigDecimal(dntTimeList.get((size - 1))).divide(new BigDecimal(2), 2, RoundingMode.HALF_UP);
            } else {
                //加0.0是为了把int转成double类型，否则除以2会算错
                median = new BigDecimal(dntTimeList.get(size / 2 - 1) + dntTimeList.get(size / 2)).divide(new BigDecimal(2), 2, RoundingMode.HALF_UP);
            }
            dntResult.setMedian(median);
            list.add(dntResult);
        }
        return list.stream().sorted(Comparator.comparing(QuaStatisticsVo.OptReslut::getXTime)).collect(Collectors.toList());
    }

    @Override
    public List<QuaStatisticsVo.DntResult> queryDnt(QuaStatisticsDto.QueryZhibiao amsHomeDto) {
        List<QuaStatisticsVo.DntResult> list = new ArrayList<>();
        List<QuaStatisticsVo.DntList> dntList = quaStatisticsDao.selectDnt(amsHomeDto.getTimeType(), amsHomeDto.getStartTime(), amsHomeDto.getEndTime());
        if (CollectionUtil.isEmpty(dntList)) {
            return null;
        }
        Map<String, List<QuaStatisticsVo.DntList>> map = dntList.stream().collect(Collectors.groupingBy(QuaStatisticsVo.DntList::getDateTime));
        for (Map.Entry<String, List<QuaStatisticsVo.DntList>> entry : map.entrySet()) {
            if (CollectionUtil.isEmpty(entry.getValue())) {
                continue;
            }
            QuaStatisticsVo.DntResult dntResult = new QuaStatisticsVo.DntResult();
            //统计每天/月/年的 DNT平均时间
            long sum = entry.getValue().stream().mapToLong(QuaStatisticsVo.DntList::getDnt).sum() / entry.getValue().size();
            dntResult.setDate(entry.getKey());
            dntResult.setAverage(new BigDecimal(sum));
            //获取中位数
            BigDecimal median = new BigDecimal(0);
            List<Long> dntTimeList = entry.getValue().stream().map(QuaStatisticsVo.DntList::getDnt).collect(Collectors.toList());
            Collections.sort(dntTimeList);
            int size = dntTimeList.size();
            if (size % 2 == 1) {
                median = new BigDecimal(dntTimeList.get((size - 1))).divide(new BigDecimal(2 * 1000), 2, RoundingMode.HALF_UP);
            } else {
                //加0.0是为了把int转成double类型，否则除以2会算错
                median = new BigDecimal(dntTimeList.get(size / 2 - 1) + dntTimeList.get(size / 2)).divide(new BigDecimal(2 * 1000), 2, RoundingMode.HALF_UP);
            }
            dntResult.setMedian(median);
            list.add(dntResult);
        }
        return list.stream().sorted(Comparator.comparing(QuaStatisticsVo.DntResult::getDate)).collect(Collectors.toList());
    }

    private QuaStatisticsVo.BqpgResult queryBqpgResult(List<QuaStatisticsVo.NcxLvList> list, QuaStatisticsDto.QueryPatient amsHomeDto) {
        QuaStatisticsVo.BqpgResult bqpgResult = new QuaStatisticsVo.BqpgResult();
        Map<Long, List<QuaStatisticsVo.NcxLvList>> map = list.stream().collect(Collectors.groupingBy(QuaStatisticsVo.NcxLvList::getFirstAidId));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //脑梗死患者神经功能缺损评估率  = 入院时行神经功能缺损NIHSS评估的脑梗死患者数 / 同期住院脑梗死患者总数
        //入院时行神经功能缺损NIHSS评估的脑梗死患者数
        int ryqspghzs = 0;
        //同期住院脑梗死患者总数
        int tqzyngshzzs = 0;
        //发病24 h内脑梗死患者急诊就诊45 min内临床实验室检查完成率= 发病24 h内急诊就诊脑梗死患者45 min内获得临床实验室诊断信息的人数 / 同期发病24 h内急诊就诊行实验室检查的脑梗死患者总数
        //发病24 h内急诊就诊脑梗死患者45 min内获得临床实验室诊断信息的人数
        int zdxxrs = 0;
        //同期发病24 h内急诊就诊行实验室检查的脑梗死患者总数
        int tqngshzrs = 0;
        //发病24 h内脑梗死患者急诊就诊30 min内完成头颅CT影像学检查率 = 发病24 h内急诊就诊的脑梗死患者30 min内获得头颅CT影像学诊断信息的人数 / 同期发病24 h内急诊就诊行头颅CT影像学检查的脑梗死患者总数
        //发病24 h内急诊就诊的脑梗死患者30 min内获得头颅CT影像学诊断信息的人数
        int tlctrs = 0;
        // 同期发病24 h内急诊就诊行头颅CT影像学检查的脑梗死患者总数
        int tqtlctzs = 0;
        //发病24 h内脑梗死患者血管内治疗术前影像学评估率 = 发病24 h内脑梗死患者行血管内治疗术前行影像学评估人数 / 同期发病24 h内脑梗死患者行血管内治疗人数
        //发病24 h内脑梗死患者行血管内治疗术前行影像学评估人数
        int xgnzlsqyxxpgrs = 0;
        //同期发病24 h内脑梗死患者行血管内治疗人数
        int tqngsxgnzlrs = 0;
        for (Map.Entry<Long, List<QuaStatisticsVo.NcxLvList>> entry : map.entrySet()) {
            List<QuaStatisticsVo.NcxLvList> value = entry.getValue();
            if (CollectionUtil.isEmpty(value)) {
                continue;
            }
            Map<String, List<QuaStatisticsVo.NcxLvList>> codeMap = value.stream().collect(Collectors.groupingBy(QuaStatisticsVo.NcxLvList::getCode));
            Long key = entry.getKey();
            boolean flag = false;
            for (QuaStatisticsVo.NcxLvList qsfxList : value) {
                if (qsfxList.isNgsFlag()) {
                    tqzyngshzzs += 1;
                }
                if (!qsfxList.isJmrsFlag()) {
                    ryqspghzs += 1;
                }
            }
            //判断发病24 h内急诊就诊
            //发病时间
            List<QuaStatisticsVo.NcxLvList> fbsjList = codeMap.get(JBXX_FBSJ.getKey());
            //到院时间
            List<QuaStatisticsVo.NcxLvList> dysjList = codeMap.get(ZL_FBQK_DY_TIME.getKey());
            if (CollectionUtil.isNotEmpty(fbsjList) && CollectionUtil.isNotEmpty(dysjList)) {
                QuaStatisticsVo.NcxLvList fbsj = fbsjList.get(0);
                QuaStatisticsVo.NcxLvList dysj = dysjList.get(0);
                if (StrUtil.isNotEmpty(fbsj.getAnswer()) && StrUtil.isNotEmpty(dysj.getAnswer())) {
                    try {
                        Date parseTime = sdf.parse(fbsj.getAnswer());
                        Date dysjTime = sdf.parse(dysj.getAnswer());
                        if (dysjTime.getTime() - parseTime.getTime() > 0 && dysjTime.getTime() - parseTime.getTime() <= 24 * 60 * 60) {

                        }
                    } catch (Exception e) {
                        log.error("发病、到院时间转换错误");
                    }
                }
            }
//            bqpgResult.set
        }
        return bqpgResult;
    }

    private QuaStatisticsVo.JxnxgzgzzlnlResult queryJxnxgzgzzlnlResult(List<QuaStatisticsVo.NcxLvList> list, QuaStatisticsDto.QueryPatient amsHomeDto) {
        QuaStatisticsVo.JxnxgzgzzlnlResult bqpgResult = new QuaStatisticsVo.JxnxgzgzzlnlResult();
        //脑出血患者吞咽功能筛查率  = 进食、水前进行吞咽功能筛查的住院脑出血患者数 / 同期住院脑出血患者总数
        Map<Long, List<QuaStatisticsVo.NcxLvList>> map = list.stream().collect(Collectors.groupingBy(QuaStatisticsVo.NcxLvList::getFirstAidId));
        //同期发病6 h内到院的急性脑出血患者总数
        int ncxhzs = 0;
        //发病6h内给予降压药物治疗的急性脑出血患者数
        int jyyhzs = 0;
        for (Map.Entry<Long, List<QuaStatisticsVo.NcxLvList>> entry : map.entrySet()) {
            List<QuaStatisticsVo.NcxLvList> value = entry.getValue();
            Long key = entry.getKey();
            boolean flag = false;
            for (QuaStatisticsVo.NcxLvList qsfxList : value) {

            }
        }
        return bqpgResult;
    }

    private QuaStatisticsVo.Gfhzlyyfbfz queryGfhzlyyfbfz(List<QuaStatisticsVo.NcxLvList> list, QuaStatisticsDto.QueryPatient amsHomeDto) {
        QuaStatisticsVo.Gfhzlyyfbfz bqpgResult = new QuaStatisticsVo.Gfhzlyyfbfz();
        //脑出血患者吞咽功能筛查率  = 进食、水前进行吞咽功能筛查的住院脑出血患者数 / 同期住院脑出血患者总数
        Map<Long, List<QuaStatisticsVo.NcxLvList>> map = list.stream().collect(Collectors.groupingBy(QuaStatisticsVo.NcxLvList::getFirstAidId));
        //同期发病6 h内到院的急性脑出血患者总数
        int ncxhzs = 0;
        //发病6h内给予降压药物治疗的急性脑出血患者数
        int jyyhzs = 0;
        for (Map.Entry<Long, List<QuaStatisticsVo.NcxLvList>> entry : map.entrySet()) {
            List<QuaStatisticsVo.NcxLvList> value = entry.getValue();
            Long key = entry.getKey();
            boolean flag = false;
            for (QuaStatisticsVo.NcxLvList qsfxList : value) {
                //判断脑出血
                if (!flag) {
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZLLX_TYPE.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "3".equals(qsfxList.getAnswer()))
                            || (ZL_FBQK_NCX.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        ncxhzs += 1;
                        flag = true;
                    }
                } else {
                    //如是脑出血，则判断是否进行康复治疗
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZL_KFZL_IS.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        jyyhzs += 1;
                        break;
                    }
                }
            }
        }
        return bqpgResult;
    }

    private QuaStatisticsVo.Ejyf queryEjyf(List<QuaStatisticsVo.NcxLvList> list, QuaStatisticsDto.QueryPatient amsHomeDto) {
        QuaStatisticsVo.Ejyf bqpgResult = new QuaStatisticsVo.Ejyf();
        //脑出血患者吞咽功能筛查率  = 进食、水前进行吞咽功能筛查的住院脑出血患者数 / 同期住院脑出血患者总数
        Map<Long, List<QuaStatisticsVo.NcxLvList>> map = list.stream().collect(Collectors.groupingBy(QuaStatisticsVo.NcxLvList::getFirstAidId));
        //同期发病6 h内到院的急性脑出血患者总数
        int ncxhzs = 0;
        //发病6h内给予降压药物治疗的急性脑出血患者数
        int jyyhzs = 0;
        for (Map.Entry<Long, List<QuaStatisticsVo.NcxLvList>> entry : map.entrySet()) {
            List<QuaStatisticsVo.NcxLvList> value = entry.getValue();
            Long key = entry.getKey();
            boolean flag = false;
            for (QuaStatisticsVo.NcxLvList qsfxList : value) {
                //判断脑出血
                if (!flag) {
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZLLX_TYPE.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "3".equals(qsfxList.getAnswer()))
                            || (ZL_FBQK_NCX.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        ncxhzs += 1;
                        flag = true;
                    }
                } else {
                    //如是脑出血，则判断是否进行康复治疗
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZL_KFZL_IS.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        jyyhzs += 1;
                        break;
                    }
                }
            }
        }
        return bqpgResult;
    }

    private QuaStatisticsVo.Jjzb queryJjzb(List<QuaStatisticsVo.NcxLvList> list, QuaStatisticsDto.QueryPatient amsHomeDto) {
        QuaStatisticsVo.Jjzb bqpgResult = new QuaStatisticsVo.Jjzb();
        //脑出血患者吞咽功能筛查率  = 进食、水前进行吞咽功能筛查的住院脑出血患者数 / 同期住院脑出血患者总数
        Map<Long, List<QuaStatisticsVo.NcxLvList>> map = list.stream().collect(Collectors.groupingBy(QuaStatisticsVo.NcxLvList::getFirstAidId));
        //同期发病6 h内到院的急性脑出血患者总数
        int ncxhzs = 0;
        //发病6h内给予降压药物治疗的急性脑出血患者数
        int jyyhzs = 0;
        for (Map.Entry<Long, List<QuaStatisticsVo.NcxLvList>> entry : map.entrySet()) {
            List<QuaStatisticsVo.NcxLvList> value = entry.getValue();
            Long key = entry.getKey();
            boolean flag = false;
            for (QuaStatisticsVo.NcxLvList qsfxList : value) {
                //判断脑出血
                if (!flag) {
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZLLX_TYPE.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "3".equals(qsfxList.getAnswer()))
                            || (ZL_FBQK_NCX.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        ncxhzs += 1;
                        flag = true;
                    }
                } else {
                    //如是脑出血，则判断是否进行康复治疗
                    if (StrUtil.isNotEmpty(qsfxList.getCode()) && ((ZL_KFZL_IS.getKey().equals(qsfxList.getCode()) && StrUtil.isNotEmpty(qsfxList.getAnswer()) && "是".equals(qsfxList.getAnswer())))) {
                        jyyhzs += 1;
                        break;
                    }
                }
            }
        }
        return bqpgResult;
    }


    /**
     * 补齐Dnt返回数据
     *
     * @param timeType 统计方式 0:周 1:月 2:季 3:年
     * @return 补齐后的数据
     */
    private List<QuaStatisticsVo.XgzlLvResult> completionXgzlData(Integer timeType, List<QuaStatisticsVo.XgzlLvResult> xgzlResults, Date startTime, Date endTime) throws ParseException {
        //时间补齐
        Date startDate;
        Date endDate;
        DateField dateField = ERA;
        int offset = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        switch (timeType) {
            case 0:
                //起始周
                offset = 1;
                dateField = DateField.WEEK_OF_YEAR;
                if (startTime == null) {
                    startDate = new DateTime(xgzlResults.get(0).getTime());
                    endDate = new DateTime(xgzlResults.get(xgzlResults.size() - 1).getTime());
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
                break;
            case 1:
                offset = 1;
                dateField = DateField.MONTH;
                sdf = new SimpleDateFormat("yyyy-MM");
                if (startTime == null) {
                    startDate = xgzlResults.get(0).getTime();
                    endDate = xgzlResults.get(xgzlResults.size() - 1).getTime();
                } else {
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
                break;
            case 2:
                dateField = DateField.MONTH;
                offset = 3;
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                if (startTime == null) {
                    startDate = xgzlResults.get(0).getTime();
                    endDate = xgzlResults.get(xgzlResults.size() - 1).getTime();
                } else {
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
                break;
            case 3:
                dateField = DateField.YEAR;
                offset = 1;
                sdf = new SimpleDateFormat("yyyy");
                if (startTime == null) {
                    startDate = xgzlResults.get(0).getTime();
                    endDate = xgzlResults.get(xgzlResults.size() - 1).getTime();
                } else {

                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
                break;
            case 4:
                offset = 1;
                dateField = DateField.DAY_OF_MONTH;
                if (startTime == null) {
                    startDate = new DateTime(xgzlResults.get(0).getTime());
                    endDate = new DateTime(xgzlResults.get(xgzlResults.size() - 1).getTime());
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
                break;
            default:
                if (startTime == null) {
                    startDate = new DateTime();
                    endDate = new DateTime();
                } else {
                    startDate = startTime;
                    endDate = endTime;
                }
                break;
        }
        List<Date> dateList = xgzlResults.stream().map(QuaStatisticsVo.XgzlLvResult::getTime).collect(Collectors.toList());
        if (!dateList.contains(startDate)) {
            QuaStatisticsVo.XgzlLvResult result = new QuaStatisticsVo.XgzlLvResult();
            result.setTime(startDate);
            result.setTotal(0);
            result.setGailv(0);
            result.setDate(sdf.format(startDate));
            xgzlResults.add(result);
        }
        while (DateUtil.compare(startDate, endDate) < 0) {
            startDate = DateUtil.offset(startDate, dateField, offset);
            if (dateList.contains(startDate)) {
                continue;
            }
            QuaStatisticsVo.XgzlLvResult result = new QuaStatisticsVo.XgzlLvResult();
            result.setTime(startDate);
            result.setTotal(0);
            result.setGailv(0);
            result.setDate(sdf.format(startDate));
            xgzlResults.add(result);
        }
        return xgzlResults;
    }

    /**
     * 补齐Dnt返回数据
     *
     * @param timeType 统计方式 0:周 1:月 2:季 3:年
     * @return 补齐后的数据
     */
    private List<QuaStatisticsVo.NcxResult> completionNcxData(Integer timeType, List<QuaStatisticsVo.NcxResult> xgzlResults, Date startTime, Date endTime) throws ParseException {
        //时间补齐
        Date startDate;
        Date endDate;
        DateField dateField = ERA;
        int offset = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        switch (timeType) {
            case 0:
                //起始周
                offset = 1;
                dateField = DateField.WEEK_OF_YEAR;
                if (startTime == null) {
                    startDate = new DateTime(xgzlResults.get(0).getTime());
                    endDate = new DateTime(xgzlResults.get(xgzlResults.size() - 1).getTime());
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
                break;
            case 1:
                offset = 1;
                dateField = DateField.MONTH;
                sdf = new SimpleDateFormat("yyyy-MM");
                if (startTime == null) {
                    startDate = xgzlResults.get(0).getTime();
                    endDate = xgzlResults.get(xgzlResults.size() - 1).getTime();
                } else {

                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
                break;
            case 2:
                dateField = DateField.MONTH;
                offset = 3;
                sdf = new SimpleDateFormat("yyyy-MM-dd");
                if (startTime == null) {
                    startDate = xgzlResults.get(0).getTime();
                    endDate = xgzlResults.get(xgzlResults.size() - 1).getTime();
                } else {
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
                break;
            case 3:
                dateField = DateField.YEAR;
                offset = 1;
                sdf = new SimpleDateFormat("yyyy");
                if (startTime == null) {
                    startDate = xgzlResults.get(0).getTime();
                    endDate = xgzlResults.get(xgzlResults.size() - 1).getTime();
                } else {

                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
                break;
            case 4:
                offset = 1;
                dateField = DateField.DAY_OF_MONTH;
                if (startTime == null) {
                    startDate = new DateTime(xgzlResults.get(0).getTime());
                    endDate = new DateTime(xgzlResults.get(xgzlResults.size() - 1).getTime());
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
                break;
            default:
                if (startTime == null) {
                    startDate = new DateTime();
                    endDate = new DateTime();
                } else {
                    startDate = startTime;
                    endDate = endTime;
                }
                break;
        }
        List<Date> dateList = xgzlResults.stream().map(QuaStatisticsVo.NcxResult::getTime).collect(Collectors.toList());
        if (!dateList.contains(startDate)) {
            QuaStatisticsVo.NcxResult result = new QuaStatisticsVo.NcxResult();
            result.setTime(startDate);
            result.setTotal(0);
            result.setGailv(0);
            result.setDate(sdf.format(startDate));
            xgzlResults.add(result);
        }
        while (DateUtil.compare(startDate, endDate) < 0) {
            startDate = DateUtil.offset(startDate, dateField, offset);
            if (dateList.contains(startDate)) {
                continue;
            }
            QuaStatisticsVo.NcxResult result = new QuaStatisticsVo.NcxResult();
            result.setTime(startDate);
            result.setTotal(0);
            result.setGailv(0);
            result.setDate(sdf.format(startDate));
            xgzlResults.add(result);
        }
        return xgzlResults;
    }

    /**
     * 补齐Dnt返回数据
     *
     * @param timeType 统计方式 0:周 1:月 2:季 3:年
     * @return 补齐后的数据
     */
    private List<QuaStatisticsVo.RptResult> completionRptData(Integer timeType, List<QuaStatisticsVo.RptResult> rptLists, Date startTime, Date endTime) throws ParseException {

        //时间补齐
        Date startDate;
        Date endDate;
        DateField dateField = ERA;
        int offset = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        switch (timeType) {
            case 0:
                //起始周
                offset = 1;
                dateField = DateField.WEEK_OF_YEAR;
                if (startTime == null) {
                    startDate = new DateTime(rptLists.get(0).getTime());
                    endDate = new DateTime(rptLists.get(rptLists.size() - 1).getTime());
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
                break;
            case 1:
                offset = 1;
                dateField = DateField.MONTH;
                if (startTime == null) {
                    startDate = rptLists.get(0).getTime();
                    endDate = rptLists.get(rptLists.size() - 1).getTime();
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
                break;
            case 2:
                dateField = DateField.MONTH;
                offset = 3;
                if (startTime == null) {
                    startDate = rptLists.get(0).getTime();
                    endDate = rptLists.get(rptLists.size() - 1).getTime();
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
                break;
            case 3:

                dateField = DateField.YEAR;
                offset = 1;
                if (startTime == null) {
                    startDate = rptLists.get(0).getTime();
                    endDate = rptLists.get(rptLists.size() - 1).getTime();
                } else {
                    sdf = new SimpleDateFormat("yyyy");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
                break;
            case 4:
                offset = 1;
                dateField = DateField.DAY_OF_MONTH;
                if (startTime == null) {
                    startDate = new DateTime(rptLists.get(0).getTime());
                    endDate = new DateTime(rptLists.get(rptLists.size() - 1).getTime());
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
                break;
            default:
                if (startTime == null) {
                    startDate = new DateTime();
                    endDate = new DateTime();
                } else {
                    startDate = startTime;
                    endDate = endTime;
                }
                break;
        }
        List<Date> dateList = rptLists.stream().map(QuaStatisticsVo.RptResult::getTime).collect(Collectors.toList());
        if (!dateList.contains(startDate)) {
            QuaStatisticsVo.RptResult result = new QuaStatisticsVo.RptResult();
            result.setTime(startDate);
            result.setTotal(0);
            result.setOther(0);
            result.setDy90(0);
            result.setDy60(0);
            result.setDate(sdf.format(startDate));
            rptLists.add(result);
        }
        while (DateUtil.compare(startDate, endDate) < 0) {
            startDate = DateUtil.offset(startDate, dateField, offset);
            if (dateList.contains(startDate)) {
                continue;
            }
            QuaStatisticsVo.RptResult result = new QuaStatisticsVo.RptResult();
            result.setTime(startDate);
            result.setTotal(0);
            result.setOther(0);
            result.setDy90(0);
            result.setDy90(0);
            result.setDate(sdf.format(startDate));
            rptLists.add(result);
        }
        return rptLists;
    }

    /**
     * 补齐Dnt返回数据
     *
     * @param timeType 统计方式 0:周 1:月 2:季 3:年
     * @return 补齐后的数据
     */
    private List<QuaStatisticsVo.DptResult> completionDptData(Integer timeType, List<QuaStatisticsVo.DptResult> dptLists, Date startTime, Date endTime) throws ParseException {

        //时间补齐
        Date startDate;
        Date endDate;
        DateField dateField = ERA;
        int offset = 0;
        String date = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        switch (timeType) {
            case 0:
                //起始周
                offset = 1;
                dateField = DateField.WEEK_OF_YEAR;
                if (startTime == null) {
                    startDate = new DateTime(dptLists.get(0).getTime());
                    endDate = new DateTime(dptLists.get(dptLists.size() - 1).getTime());
                    date = dptLists.get(0).getDate();
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                    date = sdf.format(startDate);
                }
                break;
            case 1:
                offset = 1;
                dateField = DateField.MONTH;
                if (startTime == null) {
                    startDate = dptLists.get(0).getTime();
                    endDate = dptLists.get(dptLists.size() - 1).getTime();
                    date = dptLists.get(0).getDate();
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                    date = sdf.format(startDate);
                }
                break;
            case 2:
                dateField = DateField.MONTH;
                offset = 3;
                if (startTime == null) {
                    startDate = dptLists.get(0).getTime();
                    endDate = dptLists.get(dptLists.size() - 1).getTime();
                    date = dptLists.get(0).getDate();
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                    date = sdf.format(startDate);
                }
                break;
            case 3:

                dateField = DateField.YEAR;
                offset = 1;
                if (startTime == null) {
                    startDate = dptLists.get(0).getTime();
                    endDate = dptLists.get(dptLists.size() - 1).getTime();
                    date = dptLists.get(0).getDate();
                } else {
                    sdf = new SimpleDateFormat("yyyy");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                    date = sdf.format(startDate);
                }
                break;
            case 4:
                offset = 1;
                dateField = DateField.DAY_OF_MONTH;
                if (startTime == null) {
                    startDate = new DateTime(dptLists.get(0).getTime());
                    endDate = new DateTime(dptLists.get(dptLists.size() - 1).getTime());
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
            default:
                if (startTime == null) {
                    startDate = new DateTime();
                    endDate = new DateTime();
                    date = dptLists.get(0).getDate();
                } else {
                    startDate = startTime;
                    endDate = endTime;
                    date = dptLists.get(0).getDate();
                }
                break;
        }
        List<Date> dateList = dptLists.stream().map(QuaStatisticsVo.DptResult::getTime).collect(Collectors.toList());
        if (!dateList.contains(startDate)) {
            QuaStatisticsVo.DptResult result = new QuaStatisticsVo.DptResult();
            result.setTime(startDate);
            result.setTotal(0);
            result.setOther(0);
            result.setDy90(0);
            result.setDy60(0);
            result.setDate(sdf.format(startDate));
            dptLists.add(result);
        }
        while (DateUtil.compare(startDate, endDate) < 0) {
            startDate = DateUtil.offset(startDate, dateField, offset);
            if (dateList.contains(startDate)) {
                continue;
            }
            QuaStatisticsVo.DptResult result = new QuaStatisticsVo.DptResult();
            result.setTime(startDate);
            result.setTotal(0);
            result.setOther(0);
            result.setDy90(0);
            result.setDy90(0);
            result.setDate(sdf.format(startDate));
            dptLists.add(result);
        }
        return dptLists;
    }

    /**
     * 补齐Dnt返回数据
     *
     * @param timeType 统计方式 0:周 1:月 2:季 3:年
     * @return 补齐后的数据
     */
    private List<QuaStatisticsVo.BfzResultVo> completionSichData(Integer timeType, List<QuaStatisticsVo.BfzResultVo> dntLists, Date startTime, Date endTime) throws ParseException {

        //时间补齐
        Date startDate;
        Date endDate;
        DateField dateField = ERA;
        int offset = 0;
        String date = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        switch (timeType) {
            case 0:
                //起始周
                offset = 1;
                dateField = DateField.WEEK_OF_YEAR;
                if (startTime == null) {
                    startDate = new DateTime(dntLists.get(0).getTime());
                    endDate = new DateTime(dntLists.get(dntLists.size() - 1).getTime());
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                    date = sdf.format(startDate);
                }
                break;
            case 1:
                offset = 1;
                dateField = DateField.MONTH;
                if (startTime == null) {
                    startDate = new DateTime(dntLists.get(0).getTime());
                    endDate = new DateTime(dntLists.get(dntLists.size() - 1).getTime());
                    date = dntLists.get(0).getTime();
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                    date = sdf.format(startDate);
                }
                break;
            case 2:
                dateField = DateField.MONTH;
                offset = 3;
                if (startTime == null) {
                    startDate = dntLists.get(0).getDate();
                    endDate = dntLists.get(dntLists.size() - 1).getDate();
                    date = dntLists.get(0).getTime();
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                    date = sdf.format(startDate);
                }
                break;
            case 3:

                dateField = DateField.YEAR;
                offset = 1;
                if (startTime == null) {
                    startDate = dntLists.get(0).getDate();
                    endDate = dntLists.get(dntLists.size() - 1).getDate();
                    date = dntLists.get(0).getTime();
                } else {
                    sdf = new SimpleDateFormat("yyyy");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                    date = sdf.format(startDate);
                }
                break;
            case 4:
                offset = 1;
                dateField = DateField.DAY_OF_MONTH;
                if (startTime == null) {
                    startDate = new DateTime(dntLists.get(0).getTime());
                    endDate = new DateTime(dntLists.get(dntLists.size() - 1).getTime());
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
            default:
                if (startTime == null) {
                    startDate = new DateTime();
                    endDate = new DateTime();
                } else {
                    startDate = startTime;
                    endDate = endTime;
                }
                break;
        }
        List<Date> dateList = dntLists.stream().map(QuaStatisticsVo.BfzResultVo::getDate).collect(Collectors.toList());
        if (!dateList.contains(startDate)) {
            QuaStatisticsVo.BfzResultVo result = new QuaStatisticsVo.BfzResultVo();
            result.setTime(sdf.format(startDate));
            result.setTotal(0);
            result.setBfNum(0);
            result.setDate(startDate);
            dntLists.add(result);
        }
        while (DateUtil.compare(startDate, endDate) < 0) {
            startDate = DateUtil.offset(startDate, dateField, offset);
            if (dateList.contains(startDate)) {
                continue;
            }
            QuaStatisticsVo.BfzResultVo result = new QuaStatisticsVo.BfzResultVo();
            result.setTime(sdf.format(startDate));
            result.setTotal(0);
            result.setBfNum(0);
            result.setDate(startDate);
            dntLists.add(result);
        }
        return dntLists;
    }

    /**
     * 补齐Dnt返回数据
     *
     * @param timeType 统计方式 0:周 1:月 2:季 3:年
     * @return 补齐后的数据
     */
    private List<QuaStatisticsVo.DntResult> completionDntData(Integer timeType, List<QuaStatisticsVo.DntResult> dntLists, Date startTime, Date endTime) throws ParseException {

        //时间补齐
        Date startDate;
        Date endDate;
        DateField dateField = ERA;
        int offset = 0;
        String date = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        switch (timeType) {
            case 0:
                //起始周
                sdf = new SimpleDateFormat("yyyy-ww");
                offset = 1;
                dateField = DateField.WEEK_OF_YEAR;
                if (startTime == null) {
                    startDate = new DateTime(dntLists.get(0).getTime());
                    endDate = new DateTime(dntLists.get(dntLists.size() - 1).getTime());
                } else {
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                    date = sdf.format(startDate);
                }
                break;
            case 1:
                offset = 1;
                dateField = DateField.MONTH;
                if (startTime == null) {
                    startDate = dntLists.get(0).getTime();
                    endDate = dntLists.get(dntLists.size() - 1).getTime();
                    date = dntLists.get(0).getDate();
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                    date = sdf.format(startDate);
                }
                break;
            case 2:
                dateField = DateField.MONTH;
                offset = 3;
                if (startTime == null) {
                    startDate = dntLists.get(0).getTime();
                    endDate = dntLists.get(dntLists.size() - 1).getTime();
                    date = dntLists.get(0).getDate();
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                    date = sdf.format(startDate);
                }
                break;
            case 3:
                dateField = DateField.YEAR;
                offset = 1;
                if (startTime == null) {
                    startDate = dntLists.get(0).getTime();
                    endDate = dntLists.get(dntLists.size() - 1).getTime();
                    date = dntLists.get(0).getDate();
                } else {
                    sdf = new SimpleDateFormat("yyyy");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                    date = sdf.format(startDate);
                }
                break;
            case 4:
                offset = 1;
                dateField = DateField.DAY_OF_MONTH;
                if (startTime == null) {
                    startDate = new DateTime(dntLists.get(0).getTime());
                    endDate = new DateTime(dntLists.get(dntLists.size() - 1).getTime());
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
            default:
                if (startTime == null) {
                    startDate = new DateTime();
                    endDate = new DateTime();
                } else {
                    startDate = startTime;
                    endDate = endTime;
                }
                break;
        }
        List<Date> dateList = dntLists.stream().map(QuaStatisticsVo.DntResult::getTime).collect(Collectors.toList());
        List<Long> dateLongList = new ArrayList<>();
        for (Date date1 : dateList) {
            dateLongList.add(date1.getTime());
        }
        if (!dateLongList.contains(startDate.getTime())) {
            QuaStatisticsVo.DntResult result = new QuaStatisticsVo.DntResult();
            result.setTime(startDate);
            result.setTotal(0);
            result.setOther(0);
            result.setDy60(0);
            result.setDy45(0);
            result.setDate(sdf.format(startDate));
            dntLists.add(result);
        }
        while (DateUtil.compare(startDate, endDate) < 0) {
            startDate = DateUtil.offset(startDate, dateField, offset);
            if (dateLongList.contains(startDate.getTime())) {
                continue;
            }
            QuaStatisticsVo.DntResult result = new QuaStatisticsVo.DntResult();
            result.setTime(startDate);
            result.setTotal(0);
            result.setOther(0);
            result.setDy60(0);
            result.setDy45(0);
            result.setDate(sdf.format(startDate));
            dntLists.add(result);
        }
        return dntLists;
    }

    /**
     * 补齐Dnt返回数据
     *
     * @param timeType 统计方式 0:周 1:月 2:季 3:年
     * @return 补齐后的数据
     */
    private List<QuaStatisticsVo.JmrsResult> completionJmrsData(Integer timeType, List<QuaStatisticsVo.JmrsResult> xgzlResults, Date startTime, Date endTime) throws ParseException {

        //时间补齐
        Date startDate;
        Date endDate;
        DateField dateField = ERA;
        int offset = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        switch (timeType) {
            case 0:
                //起始周 2
                offset = 1;
                dateField = DateField.WEEK_OF_YEAR;
                if (startTime == null) {
                    startDate = new DateTime(xgzlResults.get(0).getTime());
                    endDate = new DateTime(xgzlResults.get(xgzlResults.size() - 1).getTime());
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
                break;
            case 1:
                offset = 1;
                dateField = DateField.MONTH;
                if (startTime == null) {
                    startDate = xgzlResults.get(0).getTime();
                    endDate = xgzlResults.get(xgzlResults.size() - 1).getTime();
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
                break;
            case 2:
                dateField = DateField.MONTH;
                offset = 3;
                if (startTime == null) {
                    startDate = xgzlResults.get(0).getTime();
                    endDate = xgzlResults.get(xgzlResults.size() - 1).getTime();
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
                break;
            case 3:
                dateField = DateField.YEAR;
                offset = 1;
                if (startTime == null) {
                    startDate = xgzlResults.get(0).getTime();
                    endDate = xgzlResults.get(xgzlResults.size() - 1).getTime();
                } else {
                    sdf = new SimpleDateFormat("yyyy");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
                break;
            case 4:
                offset = 1;
                dateField = DateField.DAY_OF_MONTH;
                if (startTime == null) {
                    startDate = new DateTime(xgzlResults.get(0).getTime());
                    endDate = new DateTime(xgzlResults.get(xgzlResults.size() - 1).getTime());
                } else {
                    sdf = new SimpleDateFormat("yyyy-MM-dd");
                    startDate = sdf.parse(sdf.format(startTime));
                    endDate = sdf.parse(sdf.format(endTime));
                }
                break;
            default:
                if (startTime == null) {
                    startDate = new DateTime();
                    endDate = new DateTime();
                } else {
                    startDate = startTime;
                    endDate = endTime;
                }
                break;
        }
        List<Date> dateList = xgzlResults.stream().map(QuaStatisticsVo.JmrsResult::getTime).collect(Collectors.toList());
        if (!dateList.contains(startDate)) {
            QuaStatisticsVo.JmrsResult result = new QuaStatisticsVo.JmrsResult();
            result.setTime(startDate);
            result.setTotal(0);
            result.setGailv(0);
            result.setDate(sdf.format(startDate));
            xgzlResults.add(result);
        }
        while (DateUtil.compare(startDate, endDate) < 0) {
            startDate = DateUtil.offset(startDate, dateField, offset);
            if (dateList.contains(startDate)) {
                continue;
            }
            QuaStatisticsVo.JmrsResult result = new QuaStatisticsVo.JmrsResult();
            result.setTime(startDate);
            result.setTotal(0);
            result.setGailv(0);
            result.setDate(sdf.format(startDate));
            xgzlResults.add(result);
        }
        return xgzlResults;
    }
}
